/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;

import java.util.ArrayList;

import snobject.snImage;
import snobject.snObject;
import step.Node;
import step.exceptions.STEPException;

/**
 * A ValueNode is a constant value, encapsulated in an snObject.
 */
public class ValueNode extends Node
{
	public ValueNode(String id, snObject Value)
	{
		super(id);
		SetValue(Value);
		assert(value != null);
	}

	public ValueNode(snObject Value)
	{
		super();
		SetValue(Value);
		assert(value != null);
	}

    public boolean IsEnabled()
    {
    	if (rt_evaluated || !rt_wanted)
    		return false;
    	return true;
    }

	public void Evaluate() throws EvaluationFailure
	{
		//System.out.println("Evaluated: "+this.GetID()+" value="+value);
		PushResult();
		rt_evaluated = true;
	}
	
	public String TagName() { return "value"; }
	
	public void Write(java.io.OutputStream out, int depth) throws java.io.IOException
	{
		assert(value != null);
		Indent(out, depth);
		out.write(("<" + TagName() + " id=\"" + ID + "\"").getBytes());
		
		if(!getBindTo().equals(""))
    		out.write((" bindTo=\"" + getBindTo() + "\"").getBytes());
		out.write((">\n").getBytes());
		
		value.xmlWrite(out,depth+1);
		assert(children.length == 0);
		if (types.size() > 0) {
			for (FlowType i: types.values()) {
				i.Write(out, depth+1);
			}
		}
		Indent(out, depth);
		out.write(("</" + TagName() + ">\n").getBytes());
	}
	
	/* returns a line for a DOT file to represent this node 
	 * may be overwritten by particular nodes to augment output
	 */
	public String GetDOTLine(){
		return "\"" + this.TagName() + "\\n" + this.ID + "\\n" + this.value + "\"";
	}

    
    public String toString(){
    	return "<html><center>\"" + super.ID + "\"<br>" + super.value + "</center><html>"; 
    	//return "<html><center>\"" + super.ID + "\"<br>" + "</center><html>";b
    }

	@Override
	public void validate(Graph g, ArrayList<STEPException> compilationErrors) {
		// Editor won't let a value node have a child so there's no need to check for that...
		// check type maybe?
	}
	
	@Override
    public String serializeAsSnafu(){
		if(value instanceof snImage)
			return "IMG";
		return value.toString();
    }
	
	@Override
	public Node ShallowCopy() {
		ValueNode  cn = new ValueNode(ID, value);
		Node.CopyNonRTPropertiesFromAtoB(this,cn);
		return cn;
	}
}

